package org.foreningsgatan.modular.shadows;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.foreningsgatan.modular.common.frame.DisplayImage;
import org.foreningsgatan.modular.shadows.generator.ShadowGenerator;
import org.foreningsgatan.modular.shadows.generator.ShadowGenerator.Modes;
import org.foreningsgatan.modular.shadows.model.Obstacle;
import org.foreningsgatan.modular.shadows.model.ObstacleImpl;
import org.foreningsgatan.modular.shadows.model.Shadow;
import org.foreningsgatan.modular.shadows.model.ShadowCaster;
import org.foreningsgatan.modular.shadows.model.ShadowCasterImpl;

/**
 * 
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 *
 */
public class ManualTestShadowGenerator {
	static private final int sOBSTACLES = 100;

	static private final int sMARKER_SIZE = 6;

	private List<Obstacle> mObstacles;

	private List<ShadowCaster> mShadowCasters;

	private int SIZE = 320;

	private BufferedImage mImage;
	
	private ShadowGenerator mGenerator;

	public ManualTestShadowGenerator() {
		mObstacles = new ArrayList<Obstacle>();
		mShadowCasters = new ArrayList<ShadowCaster>();
		mGenerator = new ShadowGenerator(Modes.CLOSEST);
		
		mImage = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D) mImage.getGraphics();

		Random vRandom = new Random();

		for (int i = 0; i < sOBSTACLES; i++) {
			double vX = vRandom.nextDouble() * SIZE;
			double vY = vRandom.nextDouble() * SIZE;

			g.drawOval((int) (vX - sMARKER_SIZE / 2),
					(int) (vY - sMARKER_SIZE / 2), sMARKER_SIZE, sMARKER_SIZE);

			mObstacles.add(new ObstacleImpl(vX, vY, null));
		}
		
		mShadowCasters.add(new ShadowCasterImpl(SIZE / 2, SIZE / 2, SIZE / 2));

		List<Shadow> vShadows = mGenerator.render(mShadowCasters, mObstacles);
		
		for (Shadow vShadow: vShadows) {
			double vLength = vShadow.getIntensity() * sMARKER_SIZE * 5;
			double vX1 = vShadow.getObstacle().getX();
			double vY1 = vShadow.getObstacle().getY();
			double vX2 = vX1 + Math.cos(vShadow.getAngle()) * vLength;
			double vY2 = vY1 + Math.sin(vShadow.getAngle()) * vLength;
			
			g.drawLine((int) vX1, (int) vY1, (int) vX2, (int) vY2);
		}
		
		new DisplayImage(mImage);
	}
	
	public static void main(String[] args) {
		new ManualTestShadowGenerator();
	}
}